package com.ewanghuiju.app.presenter.pintuan;

import com.ewanghuiju.app.base.RxPresenter;
import com.ewanghuiju.app.base.contract.pintuan.PintuanOrderDetailsContract;
import com.ewanghuiju.app.model.DataManager;
import com.ewanghuiju.app.model.bean.response.PintuanOrderDetailsResponBean;
import com.ewanghuiju.app.model.bean.response.ShareResponBean;
import com.ewanghuiju.app.model.http.response.BaseResponse;
import com.ewanghuiju.app.model.http.response.Optional;
import com.ewanghuiju.app.util.RxUtil;
import com.ewanghuiju.app.widget.CommonSubscriber;

import javax.inject.Inject;

/**
 * @author sj  qq:751943509
 * @class 拼团订单详情
 * @time 2020/2/16
 */
public class PintuanOrderDetailsPresenter extends RxPresenter<PintuanOrderDetailsContract.View> implements PintuanOrderDetailsContract.Presenter {

    private DataManager mDataManager;
//    private int currentType = Constants.TYPE_ADDRESS_SELECTED;

    @Inject
    public PintuanOrderDetailsPresenter(DataManager mDataManager) {
        this.mDataManager = mDataManager;
    }

    @Override
    public void getData(String order_id) {
        addSubscribe(mDataManager.ptiorderInfo(order_id)
                        .compose(RxUtil.rxSchedulerHelper())
                        .compose(RxUtil.handleMyResultCannull())
                        .subscribeWith(new CommonSubscriber<Optional<PintuanOrderDetailsResponBean>>(mView) {
                            @Override
                            public void onNext(Optional<PintuanOrderDetailsResponBean> optional) {
                                if (mView != null)
                                    mView.showContent(optional.getIncludeNull());
                            }

                            @Override
                            public void onError(Throwable e) {
                                super.onError(e);
//                        if (mView != null) {
//                            if (e instanceof ApiException) {
//                                mView.showPayError(((ApiException) e).getCode(), e.getMessage());
//                            }
//                        }
                            }
                        })
        );
    }

    @Override
    public void ptitakeDelivery(String order_id) {
        addSubscribe(mDataManager.ptitakeDelivery(order_id)
                .compose(RxUtil.rxSchedulerHelper())
                .compose(RxUtil.handleMyResultCannull())
                .subscribeWith(new CommonSubscriber<Optional<BaseResponse>>(mView) {
                    @Override
                    public void onNext(Optional<BaseResponse> optional) {
                        //收货成功
                        if (mView != null)
                            mView.ptitakeDelivery();
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                    }
                })
        );
    }


    /**
     * 获取分享信息
     *
     * @param goodsid
     * @param groupid
     */
    @Override
    public void getPtiptGoodsShare(String goodsid, String groupid) {
        addSubscribe(mDataManager.ptiptGoodsShare(goodsid, groupid)
                .compose(RxUtil.rxSchedulerHelper())
                .compose(RxUtil.handleMyResultCannull())
                .subscribeWith(new CommonSubscriber<Optional<ShareResponBean>>(mView) {
                    @Override
                    public void onNext(Optional<ShareResponBean> optional) {
                        if (mView != null)
                            mView.showPtiptGoodsShare(optional.getIncludeNull());
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                    }
                })
        );
    }

}
